*******************************************************************************
* Load data
*******************************************************************************

use dataset_voters, clear
keep if sample == "mturk"

*******************************************************************************
* Covariates
*******************************************************************************

local convictionrate convictionrate_3 convictionrate_4 ///
	convictionrate_5 convictionrate_6 convictionrate_7 convictionrate_8
local ageq ageq_2 ageq_3 ageq_4 ageq_5
local race race_2 race_3 race_4
local education education_2 education_3

#delimit;

local X 
	`convictionrate'
	`ageq'
	sex_2
	`race'
	`education'
	;

#delimit cr

*******************************************************************************
* Make interactions
*******************************************************************************

tabulate ideology, gen(ideology_cand_)
tabulate ideology_self, gen(ideology_self_)
tabulate distance, gen(distance_)
forvalues i = 1(1)7 {
	gen adaXideology_self_`i' = ada*ideology_self_`i'
	label variable ideology_cand_`i' "Ideology=`i'"
	label variable ideology_self_`i' "Ideology=`i'"
	label variable distance_`i' "Distance=`i'"
	label variable adaXideology_self_`i' "Experienced X Ideology=`i'"
}
drop ideology_cand_1 adaXideology_self_1 distance_1 ideology_self_1

*******************************************************************************
* First regression
*******************************************************************************

//keep if ideology_self > 0
gen index = _n
gen B = .
gen Bcf = .
gen SE = .
gen SEcf = .

xi: reg vote ada distance_* `X', cl(responseid)

replace B = _b[ada] if index == 1
replace SE = _se[ada] if index == 1
su B
local myb = r(mean)

forvalues i = 2(1)7 {
	replace B = _b[distance_`i'] if index == `i'
	replace SE = _se[distance_`i'] if index == `i'
	replace Bcf = B + `myb' if index == `i'
	lincom distance_`i' + ada
	replace SEcf = r(se) if index == `i'
}

preserve

	drop if mi(B)
	
	collapse B SE Bcf SEcf, by(index)

	replace index = index - 1
	
	replace Bcf = B if index == 0
	replace SEcf = SE if index == 0
	replace B = 0 if index == 0
	
	gen UB = B + 1.96*SE if index > 0
	gen LB = B - 1.96*SE if index > 0
	gen UBcf = Bcf + 1.96*SEcf
	gen LBcf = Bcf - 1.96*SEcf

	gen p = round(2*(1-normal(abs(B/SE))), 0.001)
	gen pcf = round(2*(1-normal(abs(Bcf/SEcf))), 0.001)

	#delimit;
	
	gr tw
		(rspike UB LB index, lwid(thick) col(black))
		(sc B index, msym(O) col(black) mlab(p) mlabpos(12))
		(rspike UBcf LBcf index, col(gray))
		(sc Bcf index, col(white) msym(O) mlcol(gray) mlab(pcf)
			mlabpos(12))
		,
			yline(0, lpat(dash))
			xlab(0(1)6)
			legend(off)
			plotregion(style(none))
			ylab(, angle(horiz))
			xtitle("Ideological Distance")
			ytitle("Effect on Vote Choice")
			text(0.15 1.9 "+Experience Effect", col(gray))
			;
	
	#delimit cr

restore

*******************************************************************************
* Output
*******************************************************************************

gr export "_output/figureS9_a.pdf", replace

*******************************************************************************
* Second regression
*******************************************************************************

drop B SE index

gen B = .
gen SE = .
gen index = _n

reg vote ada ideology_self_* adaXideology_self* `X', cl(responseid)

replace B = _b[ada] if index == 1
replace SE = _se[ada] if index == 1

forvalues i = 2(1)7 {
	lincom ada + adaXideology_self_`i'
	replace B = r(estimate) if index == `i'
	replace SE = r(se) if index == `i'
}

preserve

	keep if !mi(B)

	collapse B SE, by(index)

	gen UB = B + 1.96*SE
	gen LB = B - 1.96*SE

	gen p = round(2*(1-normal(abs(B/SE))), 0.001)

	#delimit;

	gr tw
		(sc B index, col(black) mlab(p) mlabpos(12))
		(rspike UB LB index, col(black))
		,
			plotregion(style(none))
			ylab(, angle(horiz))
			xtitle("Respondent Conservatism")
			ytitle("Marginal Effect of Experience")
			xlab(1(1)7)
			legend(off)
			;
	#delimit cr
	
restore

*******************************************************************************
* Output
*******************************************************************************

gr export "_output/figureS9_b.pdf", replace

*******************************************************************************
* End
*******************************************************************************


